/**
 * 时间戳转换
 * @param time 时间戳
 * @returns {Function} 2000-00-00 00:00:00
 * @constructor
 */
const times = (time) => {
	var now = new Date(time);
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	var date = now.getDate();
	var hour = now.getHours();
	var minute = now.getMinutes();
	var second = now.getSeconds();
	if (minute < 10) {
		minute = '0' + minute
	}
	if (second < 10) {
		second = '0' + second
	}
	if (hour < 10) {
		hour = '0' + hour
	}
	if (month < 10) {
		month = '0' + month
	}
	if (date < 10) {
		date = '0' + date
	}
	return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
};

/**
 * 时间戳转换
 * @param time 时间戳
 * @returns {Function} 2000 00 00
 * @constructor
 */
const timesa = (time) => {
	var now = new Date(time);
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	var date = now.getDate();

	if (month < 10) {
		month = '0' + month
	}
	if (date < 10) {
		date = '0' + date
	}
	return year.toString() + month.toString() + date.toString();
};

/**
 * 获取日期的今天，明天，后天
 * @param value 开始计算的时间戳 默认值当天
 * @param addDayCount -1是昨天，0是今天，1是后一天
 * @returns {Function} 2000 00 00
 */
function getDateStr(value, addDayCount) {
	let date
	if (value) {
		date = new Date(value);
	} else {
		date = new Date();
	}
	date.setDate(date.getDate() + addDayCount); //获取AddDayCount天后的日期 
	let y = date.getFullYear();
	let m = date.getMonth() + 1; //获取当前月份的日期 
	let d = date.getDate();
	if (m < 10) {
		m = '0' + m;
	};
	if (d < 10) {
		d = '0' + d;
	};
	// console.log(y + "-" + m + "-" + d)
	return y + "-" + m + "-" + d;
}


/**
 * 获取今天起后几天
 * @param value 开始计算的时间戳 默认值当天
 * @param index 往后推进几天
 * @param type1  是否需要转化今天天明天后天  true / false
 * @param type2  是否需要添加周  true / false
 * @returns [ '01/01 周一' ,'01/02 周二' ] 
 */
function datetime(value, index, type1 = false, type2 = false) {
	let timestamp
	if (value) {
		timestamp = new Date.parse(value);
	} else {
		timestamp = Date.parse(new Date());
	}
	let tempDate = new Date(timestamp)
	let days = ''
	let week = ''
	let yue = ''
	let ri = ''
	let date = ''
	for (let i = 1; i < (index + 1); i++) {
		days = tempDate.getDay();
		yue = new Date(tempDate).getMonth() + 1 //=>0
		ri = new Date(tempDate).getDate() //=>6
		switch (days) {
			case 1:
				week = ' (周一)';
				break;
			case 2:
				week = ' (周二)';
				break;
			case 3:
				week = ' (周三)';
				break;
			case 4:
				week = ' (周四)';
				break;
			case 5:
				week = ' (周五)';
				break;
			case 6:
				week = ' (周六)';
				break;
			case 0:
				week = ' (周日)';
				break;
		}
		if (type1) {
			let jmh = i == 1 ? '今天' : i == 2 ? '明天' : i == 3 ? '后天' : yue + '/' + ri
		} else {
			let jmh = yue + '/' + ri
		}
		if (type2) {
			date.push(jmh + week)
		} else {
			date.push(jmh)
		}

		tempDate = new Date(timestamp + (86400000 * i))
	}
	return date
}
module.exports = {
	times,
	timesa,
	getDateStr,
	datetime
}